home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 January: Mac OS SDK / Dev.CD Jan 97 SDK2.toast / Development Kits (Disc 2) / OpenDoc Development Framework / ODFDev / ODF / OS / FWODMisc / FWRect.h < prev    next >
Encoding:
Text File  |  1996-09-17  |  13.4 KB  |  376 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                FWRect.h
  4. //    Release Version:    $ ODF 2 $
  5. //
  6. //    Copyright:            (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9.  
  10. #ifndef FWRECT_H
  11. #define FWRECT_H
  12.  
  13. #ifndef FWPOINT_H
  14. #include "FWPoint.h"
  15. #endif
  16.  
  17. #ifndef SLPTRECT_H
  18. #include "SLPtRect.h"
  19. #endif
  20.     
  21. //========================================================================================
  22. //    Typedefs
  23. //========================================================================================
  24.  
  25. typedef unsigned short FW_PointSelector;
  26.  
  27. const FW_PointSelector FW_kTopLeft = 0;
  28. const FW_PointSelector FW_kBotRight = 1;
  29.  
  30. //========================================================================================
  31. //    Forward Class Declarations
  32. //========================================================================================
  33.  
  34. class FW_CRect;
  35.  
  36. //========================================================================================
  37. //    Global
  38. //========================================================================================
  39.  
  40. extern const FW_CRect        FW_kZeroRect;
  41.  
  42. //========================================================================================
  43. //    Global operators << and >>
  44. //========================================================================================
  45.  
  46. FW_CWritableStream& operator<<(FW_CWritableStream& stream, const FW_SRect& rect);
  47. FW_CReadableStream& operator>>(FW_CReadableStream& stream, FW_SRect& rect);
  48.  
  49. //========================================================================================
  50. //    class FW_CPlatformRect
  51. //========================================================================================
  52.  
  53. class FW_CPlatformRect : public FW_PlatformRect
  54. {
  55. public:
  56.     FW_CPlatformRect();
  57.     
  58.     FW_CPlatformRect(FW_PlatformCoordinate l, FW_PlatformCoordinate t, 
  59.                     FW_PlatformCoordinate r, FW_PlatformCoordinate b);
  60.                     
  61.     FW_CPlatformRect(const FW_CPlatformPoint& topLeft, const FW_CPlatformPoint& botRight);
  62.     
  63.     FW_CPlatformRect(const FW_PlatformRect& plfmRect);
  64.     FW_CPlatformRect(const FW_CPlatformRect& other);
  65.     FW_CPlatformRect(const FW_SRect& rect);
  66.         
  67.     FW_CPlatformRect& operator=(const FW_CPlatformRect& other);
  68.     FW_CPlatformRect& operator=(const FW_PlatformRect& plfmRect);
  69.     FW_CPlatformRect& operator=(const FW_SRect& rect);
  70.     
  71.     const FW_CPlatformPoint&     TopLeft() const;
  72.     FW_CPlatformPoint&            TopLeft();
  73.     const FW_CPlatformPoint&     BotRight() const;
  74.     FW_CPlatformPoint&            BotRight();
  75.  
  76.     FW_CPlatformPoint             TopRight() const;        // Note: it is impossible to return non-const reference
  77.     FW_CPlatformPoint             BotLeft() const;        // Note: it is impossible to return non-const reference
  78.     
  79.     FW_Boolean            operator==(const FW_CPlatformRect& rect) const;
  80.     FW_Boolean            operator!=(const FW_CPlatformRect& rect) const;
  81.  
  82.     void Set(FW_PlatformCoordinate l, FW_PlatformCoordinate t, 
  83.             FW_PlatformCoordinate r, FW_PlatformCoordinate b);
  84. };
  85.  
  86. inline FW_CPlatformRect::FW_CPlatformRect() {left = right = top = bottom = 0;}
  87.  
  88. //========================================================================================
  89. // Transformation utilities
  90. //========================================================================================
  91.  
  92. void            FW_Transform(Environment* ev, FW_SRect& rect, ODTransform* transform);
  93. void            FW_InverseTransform(Environment* ev, FW_SRect& rect, ODTransform* transform);
  94.  
  95. FW_CRect        FW_TransformCopy(Environment* ev, const FW_SRect& rect, ODTransform* transform);
  96. FW_CRect        FW_InverseTransformCopy(Environment* ev, const FW_SRect& rect, ODTransform* transform);
  97.  
  98. //========================================================================================
  99. // class FW_CRect
  100. //========================================================================================
  101.  
  102. class FW_CRect : public FW_SRect
  103. {
  104. //----------------------------------------------------------------------------------------
  105. //    Constructors/Destructors
  106. //
  107. public:
  108.     FW_CRect();
  109.     FW_CRect(FW_Fixed l, FW_Fixed t, FW_Fixed r, FW_Fixed b);
  110.  
  111.     FW_CRect(const FW_PlatformRect& plfmRect);
  112.     FW_CRect(const FW_SRect& rect);
  113.     FW_CRect(const FW_CRect& other);
  114.     FW_CRect(const ODRect& odRect);
  115.     
  116.     FW_CRect(FW_CReadableStream& stream);
  117.     
  118.     FW_CRect(const FW_SPoint& topLeft, const FW_SPoint& botRight);
  119.     FW_CRect(const FW_SPoint& startPoint, const FW_SPoint& endPoint, FW_Fixed penSize);
  120.     FW_CRect(const FW_SPoint& topLeft, FW_Fixed width, FW_Fixed height);
  121.  
  122. //----------------------------------------------------------------------------------------
  123. //    operators
  124. //
  125. public:
  126.     // ----- Assignement -----    
  127.     FW_CRect& operator=(const FW_SRect& sRect);
  128.     FW_CRect& operator=(const FW_CRect& other);
  129.     FW_CRect& operator=(const ODRect& odRect);
  130.     FW_CRect& operator=(const FW_PlatformRect& plfmRect);
  131.  
  132.     // ----- Modification -----
  133.     void    operator&=(const FW_SRect& rect);                // Intersect with rectangle
  134.     void    operator|=(const FW_SRect& rect);                // Union with rectangle
  135.     void    operator|=(const FW_SPoint& point);                // Expand to fit point
  136.     
  137.     // ----- Conversion -----
  138. //     It is too dangerous to have automatic conversion. Use AsPlatformRect.
  139. //    operator FW_PlatformRect() const;
  140.  
  141.     operator ODRect() const
  142.         { return * (ODRect*) this; }
  143.     
  144.     operator ODRect*()
  145.         { return (ODRect*) this; }
  146.     
  147.     operator FW_SRect*() const
  148.         { return (FW_SRect*) this; }
  149.  
  150.     // ----- Array -----
  151.     FW_CPoint& operator[](FW_PointSelector sel);        
  152.     const FW_CPoint& operator[](FW_PointSelector sel) const;
  153.     
  154.     // ----- Testing -----
  155.     FW_Boolean    operator==(const FW_SRect& sRect) const;
  156.     FW_Boolean    operator!=(const FW_SRect& sRect) const
  157.                             {return !(*this==sRect);}
  158.     
  159. //----------------------------------------------------------------------------------------
  160. //    New API
  161. //
  162. public:
  163.     // ----- Conversion -----
  164.     FW_CPlatformRect    AsPlatformRect() const;
  165.  
  166.     // ----- Modification -----
  167.     void            Clear();
  168.     void            Set(FW_Fixed left, FW_Fixed top, FW_Fixed right, FW_Fixed bottom);
  169.     void            Set(const FW_SPoint& topLeft, FW_Fixed width, FW_Fixed height);
  170.     void            Set(const FW_SPoint& topLeft, const FW_SPoint& botRight);
  171.     void            SetInt(short left, short top, short right, short bottom);
  172.     void            Offset(FW_Fixed x, FW_Fixed y);
  173.     void            Offset(const FW_SPoint& offset);
  174.     void            Place(FW_Fixed x, FW_Fixed y);
  175.     void            Place(const FW_SPoint& offset);
  176.     void            Inset(FW_Fixed x, FW_Fixed y);
  177.     void            Inset(FW_Fixed inset)
  178.                         {Inset(inset, inset);}
  179.     void            Map(const FW_SRect& srcRect, const FW_SRect& dstRect);
  180.     void            PlaceInCenterOf(const FW_SRect& srcRect);
  181.     
  182.     void            Sort();
  183.     
  184.     FW_CRect&        operator+=(const FW_SPoint& pt);
  185.     FW_CRect        operator+(const FW_SPoint& pt) const;
  186.  
  187.     FW_CRect&        operator-=(const FW_SPoint& pt);
  188.     FW_CRect        operator-(const FW_SPoint& pt) const;
  189.  
  190.     // ----- OpenDoc Transform -----
  191.         // Modify the rectangle
  192.     void            Transform(Environment* ev, ODTransform* transform)
  193.                         { FW_Transform(ev, *this, transform); }
  194.     void            InverseTransform(Environment* ev, ODTransform* transform)
  195.                         { FW_InverseTransform(ev, *this, transform); }
  196.  
  197.         // Return a modified copy
  198.     FW_CRect        TransformCopy(Environment* ev, ODTransform* transform) const
  199.                         { return FW_TransformCopy(ev, *this, transform); }
  200.     FW_CRect        InverseTransformCopy(Environment* ev, ODTransform* transform) const
  201.                         { return FW_InverseTransformCopy(ev, *this, transform); }
  202.  
  203.     // ----- Accessors -----
  204.     
  205.     const FW_CPoint&     TopLeft() const
  206.                                 {return *(FW_CPoint*)&left;}
  207.     FW_CPoint&            TopLeft()
  208.                                 {return *(FW_CPoint*)&left;}
  209.     const FW_CPoint&     BotRight() const
  210.                                 {return *(FW_CPoint*)&right;}
  211.     FW_CPoint&            BotRight()
  212.                                 {return *(FW_CPoint*)&right;}
  213.     FW_Fixed            Width() const
  214.                                 {return FW_Fixed(right-left);}
  215.     FW_Fixed            Height() const
  216.                                 {return FW_Fixed(bottom-top);}
  217.     FW_Fixed            Length(FW_XYSelector selector) const;
  218.     FW_CPoint            Size() const;
  219.     
  220.     FW_CPoint             TopRight() const;        // Note: it is impossible to return a non-const reference
  221.     FW_CPoint             BotLeft() const;        // Note: it is impossible to return a non-const reference
  222.  
  223.     FW_CPoint             Center() const;
  224.  
  225.     // ----- Testing -----
  226.     FW_Boolean     IsEmpty() const;
  227.     FW_Boolean     Contains(const FW_SPoint& point) const;
  228.     FW_Boolean     Contains(const FW_SRect& rect) const;
  229.     FW_Boolean     IsIntersecting(const FW_SRect& rect) const;
  230.     
  231.     // ----- Operation on Rectangle -----
  232.     void         Intersection(const FW_SRect& rect);
  233.     void         Intersection(const FW_SRect& rect1, const FW_SRect& rect2);
  234.  
  235.     void         Union(const FW_SRect& rect);
  236.     void         Union(const FW_SRect& rect1, const FW_SRect& rect2);
  237. };
  238.  
  239. //========================================================================================
  240. //    Inlines
  241. //========================================================================================
  242.  
  243. //----------------------------------------------------------------------------------------
  244. //    FW_CPlatformRect::FW_CPlatformRect
  245. //----------------------------------------------------------------------------------------
  246. inline FW_CPlatformRect::FW_CPlatformRect(FW_PlatformCoordinate l, FW_PlatformCoordinate t, 
  247.                                             FW_PlatformCoordinate r, FW_PlatformCoordinate b)
  248. {
  249.     left = l; top = t; 
  250.     right = r; bottom = b;
  251.  
  252. //----------------------------------------------------------------------------------------
  253. //    FW_CPlatformRect::FW_CPlatformRect
  254. //----------------------------------------------------------------------------------------
  255. inline FW_CPlatformRect::FW_CPlatformRect(const FW_CPlatformPoint& topLeft, 
  256.                                           const FW_CPlatformPoint& botRight)
  257. {
  258. #ifdef FW_BUILD_MAC
  259.     left = topLeft.h; top = topLeft.v; 
  260.     right = botRight.h; bottom = botRight.v;
  261. #endif
  262. #ifdef FW_BUILD_WIN
  263.     left = topLeft.x; top = topLeft.y; 
  264.     right = botRight.x; bottom = botRight.y;
  265. #endif
  266.  
  267. //----------------------------------------------------------------------------------------
  268. //    FW_CPlatformRect::FW_CPlatformRect
  269. //----------------------------------------------------------------------------------------
  270. inline FW_CPlatformRect::FW_CPlatformRect(const FW_CPlatformRect& other)
  271. {
  272.     left = other.left; top = other.top; 
  273.     right = other.right; bottom = other.bottom;
  274.  
  275. //----------------------------------------------------------------------------------------
  276. //    FW_CPlatformRect::FW_CPlatformRect
  277. //----------------------------------------------------------------------------------------
  278. inline FW_CPlatformRect::FW_CPlatformRect(const FW_PlatformRect& plfmRect)
  279. {
  280.     left = plfmRect.left; top = plfmRect.top; 
  281.     right = plfmRect.right; bottom = plfmRect.bottom;
  282.  
  283. //----------------------------------------------------------------------------------------
  284. //    FW_CPlatformRect::operator=
  285. //----------------------------------------------------------------------------------------
  286. inline FW_CPlatformRect& FW_CPlatformRect::operator=(const FW_CPlatformRect& other)
  287. {
  288.     left = other.left; top = other.top; 
  289.     right = other.right; bottom = other.bottom;
  290.      return *this;
  291.  
  292. //----------------------------------------------------------------------------------------
  293. //    FW_CPlatformRect::operator=
  294. //----------------------------------------------------------------------------------------
  295. inline FW_CPlatformRect& FW_CPlatformRect::operator=(const FW_PlatformRect& plfmRect)
  296. {
  297.     left = plfmRect.left; top = plfmRect.top; 
  298.     right = plfmRect.right; bottom = plfmRect.bottom;
  299.      return *this;
  300.  
  301. //----------------------------------------------------------------------------------------
  302. //    FW_CPlatformRect::Set
  303. //----------------------------------------------------------------------------------------
  304. inline void FW_CPlatformRect::Set(FW_PlatformCoordinate l, FW_PlatformCoordinate t, 
  305.                                     FW_PlatformCoordinate r, FW_PlatformCoordinate b)
  306. {
  307.     left = l; top = t; 
  308.     right = r; bottom = b;
  309. }    
  310.  
  311. //----------------------------------------------------------------------------------------
  312. //    FW_CPlatformRect::TopLeft
  313. //----------------------------------------------------------------------------------------
  314. inline const FW_CPlatformPoint& FW_CPlatformRect::TopLeft() const
  315. {
  316.     return *(FW_CPlatformPoint*)this;
  317. }
  318.  
  319. //----------------------------------------------------------------------------------------
  320. //    FW_CPlatformRect::TopLeft
  321. //----------------------------------------------------------------------------------------
  322. inline FW_CPlatformPoint& FW_CPlatformRect::TopLeft()
  323. {
  324.     return *(FW_CPlatformPoint*)this;
  325. }
  326.  
  327. //----------------------------------------------------------------------------------------
  328. //    FW_CPlatformRect::BotRight
  329. //----------------------------------------------------------------------------------------
  330. inline const FW_CPlatformPoint& FW_CPlatformRect::BotRight() const
  331. {
  332. #ifdef FW_BUILD_MAC
  333.     return *(FW_CPlatformPoint*)⊥
  334. #endif
  335. #ifdef FW_BUILD_WIN
  336.     return *(FW_CPlatformPoint*)&right;
  337. #endif
  338. }
  339.  
  340. //----------------------------------------------------------------------------------------
  341. //    FW_CPlatformRect::BotRight
  342. //----------------------------------------------------------------------------------------
  343. inline FW_CPlatformPoint& FW_CPlatformRect::BotRight()
  344. {
  345. #ifdef FW_BUILD_MAC
  346.     return *(FW_CPlatformPoint*)⊥
  347. #endif
  348. #ifdef FW_BUILD_WIN
  349.     return *(FW_CPlatformPoint*)&right;
  350. #endif
  351. }
  352.  
  353. //----------------------------------------------------------------------------------------
  354. //    FW_CRect:: operator&=
  355. //----------------------------------------------------------------------------------------
  356. inline void FW_CRect:: operator&=(const FW_SRect &r)
  357. {
  358.     Intersection(r);
  359. }
  360.  
  361. //----------------------------------------------------------------------------------------
  362. //    FW_CRect:: operator|=
  363. //----------------------------------------------------------------------------------------
  364. inline void FW_CRect:: operator|= (const FW_SRect &r)
  365. {
  366.     Union(r);
  367. }
  368.  
  369. #endif
  370.